package com.jin.spring_boot_learn.concurrent.collections.delay_queue;


import java.util.concurrent.DelayQueue;

public class FoodChecker implements Runnable {
    private DelayQueue<Food> queue;

    public FoodChecker(DelayQueue<Food> queue) {
        this.queue = queue;
    }

    public void run() {
        try {
            System.out.println("开始检查!");
            boolean flag = true;
            while (flag) {
                Food food = queue.take();// 此处会阻塞,没有时过期食品时不会取出
                System.out.println(food.getFoodName() + "食品过期!保存时间:" + food.getSaveTime() + "天.");
                if (queue.isEmpty()) {
                    flag = false;
                }
            }
            System.out.println("检查完毕!");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}